﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Routing;
using System.Web.Mvc;

using Microsoft.Practices.ServiceLocation;

using Happy.ExtentionMethods;

namespace Happy.Web.Mvc
{
    /// <inheritdoc />
    public sealed class ServiceLocationDependencyResolver : IDependencyResolver
    {
        /// <inheritdoc />
        public object GetService(Type serviceType)
        {
            serviceType.MustNotNull("serviceType");

            try
            {
                return ServiceLocator.Current.GetInstance(serviceType);
            }
            catch
            {
                return null;
            }
        }

        /// <inheritdoc />
        public IEnumerable<object> GetServices(Type serviceType)
        {
            serviceType.MustNotNull("serviceType");

            try
            {
                return ServiceLocator.Current.GetAllInstances(serviceType);
            }
            catch
            {
                return new List<object>();
            }
        }
    }
}
